<template>
  <div class="page-container">
    <!-- 顶部搜索栏 -->
    <div class="search-header">
      <div class="search-bar" @click="goToSearch">
        <Find class="search-icon" />
        <span class="search-placeholder">搜索商品</span>
      </div>
    </div>

    <!-- 轮播图 -->
    <div class="banner-section">
      <nut-swiper 
        :init-page="0" 
        :pagination-visible="true" 
        :pagination-color="'#426543'"
        :auto-play="3000"
      >
        <nut-swiper-item v-for="(item, index) in banners" :key="index">
          <img :src="item.image" :alt="item.title" class="banner-image" />
        </nut-swiper-item>
      </nut-swiper>
    </div>

    <!-- 分类导航 -->
    <div class="category-nav">
      <div 
        class="category-item" 
        v-for="category in categories" 
        :key="category.id"
        @click="goToCategory(category.id)"
      >
        <div class="category-icon">
          <img :src="category.icon" :alt="category.name" />
        </div>
        <span class="category-name">{{ category.name }}</span>
      </div>
    </div>

    <!-- 限时特惠 -->
    <div class="section">
      <div class="section-header">
        <h3 class="section-title">限时特惠</h3>
        <span class="more-link" @click="goToCategory('sale')">更多 ></span>
      </div>
      <div class="product-grid">
        <div 
          class="product-card" 
          v-for="product in hotProducts" 
          :key="product.id"
          @click="goToProduct(product.id)"
        >
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
            <div class="product-tag" v-if="product.tag">{{ product.tag }}</div>
          </div>
          <div class="product-info">
            <h4 class="product-name">{{ product.name }}</h4>
            <div class="product-price">
              <span class="current-price">¥{{ product.price }}</span>
              <span class="original-price" v-if="product.originalPrice">¥{{ product.originalPrice }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 新品推荐 -->
    <div class="section">
      <div class="section-header">
        <h3 class="section-title">新品推荐</h3>
        <span class="more-link" @click="goToCategory('new')">更多 ></span>
      </div>
      <div class="product-list">
        <div 
          class="product-item" 
          v-for="product in newProducts" 
          :key="product.id"
          @click="goToProduct(product.id)"
        >
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
          </div>
          <div class="product-info">
            <h4 class="product-name">{{ product.name }}</h4>
            <p class="product-desc">{{ product.description }}</p>
            <div class="product-price">
              <span class="current-price">¥{{ product.price }}</span>
              <span class="original-price" v-if="product.originalPrice">¥{{ product.originalPrice }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Find } from '@nutui/icons-vue'

const router = useRouter()

// 轮播图数据
const banners = ref([
  {
    id: 1,
    image: 'https://picsum.photos/800/400?random=1',
    title: '限时特惠'
  },
  {
    id: 2,
    image: 'https://picsum.photos/800/400?random=2',
    title: '新品上市'
  },
  {
    id: 3,
    image: 'https://picsum.photos/800/400?random=3',
    title: '品牌专区'
  }
])

// 分类数据
const categories = ref([
  { id: 1, name: '手机数码', icon: 'https://picsum.photos/40/40?random=10' },
  { id: 2, name: '服装鞋包', icon: 'https://picsum.photos/40/40?random=11' },
  { id: 3, name: '家居生活', icon: 'https://picsum.photos/40/40?random=12' },
  { id: 4, name: '美妆护肤', icon: 'https://picsum.photos/40/40?random=13' },
  { id: 5, name: '运动户外', icon: 'https://picsum.photos/40/40?random=14' },
  { id: 6, name: '食品生鲜', icon: 'https://picsum.photos/40/40?random=15' },
  { id: 7, name: '图书文具', icon: 'https://picsum.photos/40/40?random=16' },
  { id: 8, name: '更多分类', icon: 'https://picsum.photos/40/40?random=17' }
])

// 热门商品数据
const hotProducts = ref([
  {
    id: 1,
    name: 'iPhone 15 Pro',
    price: 7999,
    originalPrice: 8999,
    image: 'https://picsum.photos/160/160?random=20',
    tag: '热销'
  },
  {
    id: 2,
    name: '华为 Mate 60',
    price: 6999,
    originalPrice: 7999,
    image: 'https://picsum.photos/160/160?random=21',
    tag: '新品'
  },
  {
    id: 3,
    name: '小米 14 Ultra',
    price: 5999,
    image: 'https://picsum.photos/160/160?random=22',
    tag: '推荐'
  },
  {
    id: 4,
    name: 'OPPO Find X7',
    price: 5499,
    image: 'https://picsum.photos/160/160?random=23'
  }
])

// 新品数据
const newProducts = ref([
  {
    id: 5,
    name: 'MacBook Pro M3',
    price: 14999,
    description: '搭载M3芯片，性能强劲',
    image: 'https://picsum.photos/120/120?random=30'
  },
  {
    id: 6,
    name: 'AirPods Pro 3',
    price: 1899,
    description: '主动降噪，空间音频',
    image: 'https://picsum.photos/120/120?random=31'
  },
  {
    id: 7,
    name: 'iPad Air 6',
    price: 4599,
    description: '轻薄便携，性能出色',
    image: 'https://picsum.photos/120/120?random=32'
  }
])

// 跳转到搜索页
const goToSearch = () => {
  router.push('/search')
}

// 跳转到分类页
const goToCategory = (categoryId) => {
  router.push(`/category?id=${categoryId}`)
}

// 跳转到商品详情
const goToProduct = (productId) => {
  router.push(`/product-detail/${productId}`)
}

onMounted(() => {
  // 页面加载完成后的逻辑
})
</script>

<style scoped>
.search-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.search-bar {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.search-icon {
  color: #999;
  font-size: 16px;
}

.search-placeholder {
  color: #999;
  font-size: 14px;
}

.banner-section {
  margin-bottom: 20px;
}

.banner-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.category-nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  padding: 20px 16px;
  background: white;
  margin-bottom: 12px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.category-icon img {
  width: 40px;
  height: 40px;
  border-radius: 8px;
}

.category-name {
  font-size: 12px;
  color: #333;
  text-align: center;
}

.section {
  background: white;
  margin-bottom: 12px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 16px 12px;
  border-bottom: 1px solid #f0f0f0;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.more-link {
  font-size: 14px;
  color: #666;
  cursor: pointer;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 16px;
}

.product-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.product-image {
  position: relative;
  height: 160px;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background: #ff4757;
  color: white;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 10px;
}

.product-info {
  padding: 12px;
}

.product-name {
  font-size: 14px;
  color: #333;
  margin: 0 0 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
}

.current-price {
  font-size: 16px;
  font-weight: 600;
  color: #ff4757;
}

.original-price {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
}

.product-list {
  padding: 16px;
}

.product-item {
  display: flex;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
}

.product-item:last-child {
  border-bottom: none;
}

.product-item .product-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.product-item .product-info {
  flex: 1;
  padding: 0;
}

.product-item .product-name {
  font-size: 16px;
  margin-bottom: 6px;
}

.product-desc {
  font-size: 12px;
  color: #666;
  margin: 0 0 8px 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
